<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Zan;
use App\Admin\Collect;
use App\Admin\Category;
use App\Admin\Single;
use App\Home\Users;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CollectController extends Controller
{
    //收藏列表显示
    public function collectshow(Request $request){
        //将分类数据库表遍历到后台
//        dump($_GET);
//        dump($request);
        if($request->search){
            $search = $request->search;
            $searchlist =DB::table('collect')
                ->where('collectstatus',$search)
                ->join('users','collect.users_id','=','users.id')
                ->join('single','collect.single_id','=','single.id')
                ->select('collect.*','users.username','single.name');
            $result=$searchlist->paginate(5);
//            return redirect('admin/CollectList?page='.$page.'&search='.$search);
        }else{
            $Sing = DB::table('collect')
                ->join('users','collect.users_id','=','users.id')
                ->join('single','collect.single_id','=','single.id')
                ->select('collect.*','users.username','single.name');
//                    ->get();
            $result = $Sing->paginate(5);
        }
        return view('admin/collect/CollectList',compact('result'));
    }
    //收藏状态修改
    public function collectedit(Request $request){
        $search=$request->search;
        $id=$_GET['id'];
        $page=$_GET['page'];
        $status=$_GET['collectstatus'];
//        dd($_GET);
        if($status==1){
            $status = 2;
        }else{
            $status =1;
        }
//        dd($status);
        $result = DB::table('collect')
            ->where('id',$id)
            ->update([
                'collectstatus'=>$status
            ]);
//        dd($_GET);
        return redirect('admin/CollectList?page='.$page.'&search='.$search);
    }
    //删除收藏
    public function collectdel(Request $request){
        $search=$request->search;
        $id = $_GET['id'];
        $page=$_GET['page'];
        $Zan = Collect::find($id);
        $SingList = $Zan->delete();
        if($search==''){
            $count = DB::table('collect')->count();
        }else{
            $count = DB::table('collect')
                    ->where('collectstatus',$search)
                    ->count();
        }
        $num = $count%5;
        if($num==0){
           $page= $page-1;
        }

        return redirect('admin/CollectList?page='.$page.'&search='.$search);
    }
}
